import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

// 解决重复点击bug
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => {});
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: Home,
  },
  {
    path: "/home",
    name: "home",
    component: Home,
  },
  {
    path: "/goods",
    name: "goods",
    component: () => import("../views/Goods.vue"),
  },
  {
    path: "/user",
    name: "user",
    component: () => import("../views/User.vue"),
    redirect: "/user/cart",
    children: [
      //  /user/cart
      { path: "cart", component: () => import("../components/user/Cart.vue") },
    ],
    // 路由独享守卫 参数同全局守卫一致
    /*  beforeEnter: (to, from, next) => {
      console.log("to:", to, "from:", from, "next:", next);
      // 则需呀获取是否存在token
      let token = localStorage.getItem("x-auth-token");
      if (token) {
        // 存在token就正常进入user页面
        next();
      } else {
        // 否则提示你还没登录
        // alert("请先登录");
        store.dispatch("showToast/asyncIsShowToast", {
          msg: "你还没有登录",
          type: "warning",
        });
      }
    }, */
  },
  {
    path: "/order",
    name: "order",
    redirect:'Error',
    component: () => import("../views/Order.vue"),
  },
  {
    path: "/free",
    name: "free",
    redirect:'Error',
    component: () => import("../views/Free.vue"),
  },
  {
    path: "/details",
    name: "details",
    component: () =>
      import(/* webpackChunkName: "details" */ "../views/Details.vue"),
  },
  {
    path: "*",
    name: "Error",
    component: () =>
      import(/* webpackChunkName: "Error" */ "../views/Error.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

export default router;
